﻿///***************************************************************
/// 시  스  템 : Updms.Ref
/// 단위시스템 : REF
/// 계      층 : Presentation Layer
/// 프로그램명 : Ref2011r
/// 파  일  명 : Ref2011r.aspx.cs
/// 설      명 : 기행기챠트
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2013-05-08 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.Page;
using SMFramework.Web.UI.WebControls.SMGridView;

using Base.AppFramework.Web.UI.CommandBar;
using Base.Dac;

using Updms.Ref.Exe;


namespace Updms.Ref
{
	public partial class Ref2011r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		public static readonly string PID = "Ref2011r";
		public static readonly string gs_path_info = "/updms/Ref/" + PID + ".aspx";

		Global global = new Global();
		AccessCheck access = new AccessCheck();
		CommCode.Com com = new CommCode.Com(LANG_SET);
		CommCode.Ref rfs = new CommCode.Ref(LANG_SET);

		//검색
		protected string kw1 = "";	//업소
		protected string kw2 = "";	//이름
		protected string kw3 = "";	//방문일(시작)
		protected string kw4 = "";	//방문일(종료)
		protected string kw5 = "";	//홈런
		protected string kw6 = "";	//내용
		protected string kw7 = "";	//정렬방법
		protected string kw8 = "";	//챠트넓이


		//메뉴 선택값
		protected string gs_sm = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
			SetClientScriptNoPostBack();
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetSearchShopId();
			SetSearchHomeRunYn();
			SetOrderByList();
			SetChartWidth();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript, SetClientScriptNoPostBack

		private void SetClientScript()
		{
			//onKeyPress이벤트 (엔터검색)
			txt_search_nick_nm.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_tour_dt_from.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_tour_dt_to.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_contents.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
		}

		//포스트백과 무관하게 이벤트 지정
		private void SetClientScriptNoPostBack()
		{
			cbl_search_shop_id.Items[0].Attributes.Add("onclick", "checkBoxListCheck('cbl_search_shop_id');");
		}

		#endregion

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))
					{
						kw1 = WebControlUtil.GetCheckBoxListValue(cbl_search_shop_id, 1);
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						WebControlUtil.SetCheckBoxListValue(cbl_search_shop_id, kw1);
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = txt_search_nick_nm.Text.Trim();
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						txt_search_nick_nm.Text = kw2;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))
					{
						if (String.IsNullOrEmpty(txt_search_tour_dt_from.Text))
						{
							kw3 = "2004-01-01";
							txt_search_tour_dt_from.Text = kw3;
						}
						else
						{
							kw3 = txt_search_tour_dt_from.Text.Trim();
						}
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						txt_search_tour_dt_from.Text = kw3;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw4"]))
					{
						if (String.IsNullOrEmpty(txt_search_tour_dt_to.Text))
						{
							kw4 = DateUtil.GetMonthEx(0);	//오늘
							txt_search_tour_dt_to.Text = kw4;
						}
						else
						{
							kw4 = txt_search_tour_dt_to.Text.Trim();
						}
					}
					else
					{
						kw4 = Server.UrlDecode(Request.QueryString["kw4"]);
						txt_search_tour_dt_to.Text = kw4;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw5"]))	// url에 값이 없으면
					{
						kw5 = ddl_search_homerun_yn.SelectedValue;
					}
					else
					{
						kw5 = Server.UrlDecode(Request.QueryString["kw5"]);
						ddl_search_homerun_yn.SelectedValue = kw5;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw6"]))	// url에 값이 없으면
					{
						kw6 = txt_search_contents.Text.Trim();
					}
					else
					{
						kw6 = Server.UrlDecode(Request.QueryString["kw6"]);
						txt_search_contents.Text = kw6;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw7"]))	// url에 값이 없으면
					{
						kw7 = ddl_search_order_by.Text.Trim();
					}
					else
					{
						kw7 = Server.UrlDecode(Request.QueryString["kw7"]);
						ddl_search_order_by.Text = kw7;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw8"]))	// url에 값이 없으면
					{
						kw8 = ddl_search_chart_width.SelectedValue;
					}
					else
					{
						kw8 = Server.UrlDecode(Request.QueryString["kw8"]);
						ddl_search_chart_width.SelectedValue = kw8;
					}
					break;
				case "SEARCH":
					kw1 = WebControlUtil.GetCheckBoxListValue(cbl_search_shop_id, 1);
					kw2 = txt_search_nick_nm.Text.Trim();
					kw3 = String.IsNullOrEmpty(txt_search_tour_dt_from.Text) ? "2004-01-01" : txt_search_tour_dt_from.Text.Trim();
					kw4 = String.IsNullOrEmpty(txt_search_tour_dt_to.Text) ? DateUtil.GetMonthEx(0) : txt_search_tour_dt_to.Text.Trim();
					kw5 = ddl_search_homerun_yn.SelectedValue;
					kw6 = txt_search_contents.Text.Trim();
					kw7 = ddl_search_order_by.Text.Trim();
					kw8 = ddl_search_chart_width.SelectedValue;
					break;
			}
		}

		#endregion

		#region 마스터 Method (SetSearchShopId, SetSearchHomeRunYn, SetOrderByList, SetChartWidth)

		private void SetSearchShopId()
		{
			DataSet lo_ds = new DataSet();
			Ref1010m_NT _Ref1010m_NT = new Ref1010m_NT();

			lo_ds.Merge(_Ref1010m_NT.GetShopInfoList());

			cbl_search_shop_id.Items.Clear();
			cbl_search_shop_id.DataSource = lo_ds;
			cbl_search_shop_id.DataMember = "UPDMS_REF_SHOP";
			cbl_search_shop_id.DataTextField = "Shop_Nm";
			cbl_search_shop_id.DataValueField = "Shop_Id";
			cbl_search_shop_id.DataBind();

			cbl_search_shop_id.Items.Insert(0, new ListItem(GetXmlMsg("STR_019"), "ALL"));	//전체선택
		}

		//홈런여부
		private void SetSearchHomeRunYn()
		{
			com.SetUseYn(ddl_search_homerun_yn);
			ddl_search_homerun_yn.Items.Insert(0, new ListItem(DDL_SELECT, ""));
		}

		//정렬방법
		private void SetOrderByList()
		{
			rfs.SetOrderByList(ddl_search_order_by);
		}

		//챠트넓이
		private void SetChartWidth()
		{
			com.SetChartWidth(ddl_search_chart_width);

			ddl_search_chart_width.SelectedValue = CHART_RESOLUTION;
		}

		#endregion

		#region CRUD (챠트..., SearchProcess)

		#region 인원별 방문
		//인원별 방문
		private void DrawChartTotalPriceNCntByGirl(DataSet lo_ds, string dt_name)
		{
			int li_count = 0;
			li_count = DataSetUtil.GetRowCount(lo_ds, dt_name);
			
			//초기화
			chart_total_price_n_cnt_by_girl.Data.Clear();

			//시리즈 갯수
			chart_total_price_n_cnt_by_girl.Data.Series = 2;

			//넓이
			chart_total_price_n_cnt_by_girl.Width = Convert.ToInt32(kw8);

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_total_price_n_cnt_by_girl.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, dt_name, i, "Total_Price");
				chart_total_price_n_cnt_by_girl.Data[1, i] = DataSetUtil.GetFldInt(lo_ds, dt_name, i, "Total_Cnt");

				//X축 라벨링
				string x_label = DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Nick_Nm") + "("+DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Shop_Nm")+")";
				chart_total_price_n_cnt_by_girl.AxisX.Labels[i] = x_label;

				//0은 안보여준다
				chart_total_price_n_cnt_by_girl.Points[0, i].PointLabels.Visible = (chart_total_price_n_cnt_by_girl.Data[0, i] > 0);
				chart_total_price_n_cnt_by_girl.Points[1, i].PointLabels.Visible = (chart_total_price_n_cnt_by_girl.Data[1, i] > 0);
			}
		}
		#endregion

		#region 업소별 방문
		//업소별 방문
		private void DrawChartTotalPriceNCntByShop(DataSet lo_ds, string dt_name)
		{
			int li_count = 0;
			li_count = DataSetUtil.GetRowCount(lo_ds, dt_name);
			
			//초기화
			chart_total_price_n_cnt_by_shop.Data.Clear();

			//시리즈 갯수
			chart_total_price_n_cnt_by_shop.Data.Series = 2;

			//넓이
			chart_total_price_n_cnt_by_shop.Width = Convert.ToInt32(kw8) - 416; //1056 - 640

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_total_price_n_cnt_by_shop.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, dt_name, i, "Total_Price");
				chart_total_price_n_cnt_by_shop.Data[1, i] = DataSetUtil.GetFldInt(lo_ds, dt_name, i, "Total_Cnt");

				//X축 라벨링
				chart_total_price_n_cnt_by_shop.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Shop_Nm");

				//0은 안보여준다
				chart_total_price_n_cnt_by_shop.Points[0, i].PointLabels.Visible = (chart_total_price_n_cnt_by_shop.Data[0, i] > 0);
				chart_total_price_n_cnt_by_shop.Points[1, i].PointLabels.Visible = (chart_total_price_n_cnt_by_shop.Data[1, i] > 0);
			}
		}
		#endregion

		#region 요일별 방문
		//요일별 방문
		private void DrawChartTourCntByWeekday(DataSet lo_ds, string dt_name)
		{
			//초기화
			chart_tour_cnt_by_weekday.Data.Clear();

			int li_count = DataSetUtil.GetRowCount(lo_ds, dt_name);

			for (int i = 0; i < li_count; i++)
			{
				string ls_label = DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Week_Day")
				+ "[" + ConversionUtil.AddComma(DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Total_Cnt"))
				+ "][" + ConversionUtil.AddComma(DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Total_Price")) + "]";

				chart_tour_cnt_by_weekday.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, dt_name, i, "Total_Cnt");
				chart_tour_cnt_by_weekday.AxisX.Labels[i] = ls_label;
			}
		}
		#endregion

		#region 인원별 홈런
		//인원별 홈런
		private void DrawChartHomeRunCntByGirl(DataSet lo_ds, string dt_name)
		{
			int li_count = 0;
			li_count = DataSetUtil.GetRowCount(lo_ds, dt_name);

			//초기화
			chart_homerun_cnt_by_girl.Data.Clear();

			//넓이
			//chart_cnt_by_homerun.Width = Convert.ToInt32(kw8);

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_homerun_cnt_by_girl.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, dt_name, i, "HomeRun_Cnt");

				//X축 라벨링
				string x_label = DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Nick_Nm") + "(" + DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Shop_Nm") + ")";
				chart_homerun_cnt_by_girl.AxisX.Labels[i] = x_label;
			}
		}
		#endregion

		#region 업소별 홈런
		//업소별 홈런
		private void DrawChartHomeRunCntByShop(DataSet lo_ds, string dt_name)
		{
			int li_count = 0;
			li_count = DataSetUtil.GetRowCount(lo_ds, dt_name);

			//초기화
			chart_homerun_cnt_by_shop.Data.Clear();

			//넓이
			//chart_homerun_cnt_by_shop.Width = Convert.ToInt32(kw8);

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_homerun_cnt_by_shop.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, dt_name, i, "HomeRun_Cnt");

				//X축 라벨링
				chart_homerun_cnt_by_shop.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Shop_Nm");
			}
		}
		#endregion

		#region 업소별 홈런율
		//업소별 홈런율
		private void DrawChartHomeRunAvgByShop(DataSet lo_ds, string dt_name)
		{
			int li_count = 0;
			li_count = DataSetUtil.GetRowCount(lo_ds, dt_name);

			//초기화
			chart_homerun_avg_by_shop.Data.Clear();

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_homerun_avg_by_shop.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, dt_name, i, "Total_Cnt");
				chart_homerun_avg_by_shop.Data[1, i] = DataSetUtil.GetFldDbl(lo_ds, dt_name, i, "HomeRun_Avg");

				//X축 라벨링
				chart_homerun_avg_by_shop.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Shop_Nm");

				//0은 안보여준다
				chart_homerun_avg_by_shop.Points[0, i].PointLabels.Visible = (chart_total_price_n_cnt_by_shop.Data[0, i] > 0);
				chart_homerun_avg_by_shop.Points[1, i].PointLabels.Visible = (chart_total_price_n_cnt_by_shop.Data[1, i] > 0);
			}
		}
		#endregion

		#region 시간대별 방문

		//업소별 홈런율
		private void DrawChartTourByHour(DataSet lo_ds, string dt_name)
		{
			int li_count = 0;
			li_count = DataSetUtil.GetRowCount(lo_ds, dt_name);

			//초기화
			chart_tour_by_hour.Data.Clear();

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_tour_by_hour.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, dt_name, i, "Total_Cnt");
				chart_tour_by_hour.Data[1, i] = DataSetUtil.GetFldDbl(lo_ds, dt_name, i, "Total_Price");

				//X축 라벨링
				chart_tour_by_hour.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Hour");

				//0은 안보여준다
				chart_tour_by_hour.Points[0, i].PointLabels.Visible = (chart_tour_by_hour.Data[0, i] > 0);
				chart_tour_by_hour.Points[1, i].PointLabels.Visible = (chart_tour_by_hour.Data[1, i] > 0);
			}
		}

		#endregion

		#region 검색
		//검색
		private void SearchProcess()
		{
			DataPack lo_dp = new DataPack();
			DataSet lo_ds = new DataSet();

			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			//챠트
			lo_dp.AddProperty("ls_shop_id", typeof(string), kw1);
			lo_dp.AddProperty("ls_nick_nm", typeof(string), kw2);
			lo_dp.AddProperty("ls_tour_dt_from", typeof(string), kw3);
			lo_dp.AddProperty("ls_tour_dt_to", typeof(string), kw4);
			lo_dp.AddProperty("ls_homerun_yn", typeof(string), kw5);
			lo_dp.AddProperty("ls_contents", typeof(string), kw6);
			lo_dp.AddProperty("ls_order_by", typeof(string), kw7);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			//DataTable명 지정
			string[] dt_name = new string[] {	"REF2011R_GIRL",
												"REF2011R_SHOP",
												"REF2011R_WEEKDAY",
												"REF2011R_HOMERUN_GIRL",
												"REF2011R_HOMERUN_SHOP",
												"REF2011R_HOMERUN_AVG_SHOP",
												"REF2011R_RECENT_TOUR_INFO",
												"REF2011R_TOUR_BY_HOUR"
											};
			using (Ref2011r_NT _Ref2011r_NT = new Ref2011r_NT())
			{
				lo_ds.Merge(_Ref2011r_NT.GetTotalChart(lo_dp, dt_name));
			}

			DrawChartTotalPriceNCntByGirl(lo_ds, dt_name[0]);
			DrawChartTotalPriceNCntByShop(lo_ds, dt_name[1]);
			DrawChartTourCntByWeekday(lo_ds, dt_name[2]);
			DrawChartHomeRunCntByGirl(lo_ds, dt_name[3]);
			DrawChartHomeRunCntByShop(lo_ds, dt_name[4]);
			DrawChartHomeRunAvgByShop(lo_ds, dt_name[5]);
			GridView1.DataSource = lo_ds.Tables[dt_name[6]];
			GridView1.DataBind();
			DrawChartTourByHour(lo_ds, dt_name[7]);
		}
		#endregion

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				//접근권한 체크
				if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
				{
					Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
				}
				//사용권한별 버튼처리
				PagePreRender.SetVisibleCtrl(this, access.CheckPgmUsingGrant(USER_DIV, PID), access.GetVisibleCtrl(PID));
			}
			ChangePageMode();
		}

		//ChangePageMode
		private void ChangePageMode()
		{
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//주요아이템
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);

				e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
			}

			//머지
			SMGridView sGv = new SMGridView(this.Page, ref GridView1);
			sGv.RowSpanGridView(GridView1, 2); //업소
			sGv.RowSpanGridView(GridView1, 3); //예명
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		#endregion
	}
}
